package com.link.play.fit.ai.ui.setting

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.google.firebase.firestore.Filter
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.base.goToAc
import com.link.play.fit.ai.bean.LogWeightBean
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.databinding.AcSetUserInfoBinding
import com.link.play.fit.ai.store.CloudStore
import com.link.play.fit.ai.store.CloudStore.DOC_LOG_WEIGHT
import com.link.play.fit.ai.store.CloudStore.DOC_USER_GUIDE
import com.link.play.fit.ai.ui.dialog.WeightControlDialog
import com.link.play.fit.ai.ui.guide.BornFg
import com.link.play.fit.ai.ui.guide.ChooseWeightFg
import com.link.play.fit.ai.ui.guide.GenderFg
import com.link.play.fit.ai.ui.guide.HeightWeightFg
import com.link.play.fit.ai.ui.guide.SettingHeightWeightFg
import com.link.play.fit.ai.ui.weightcontrol.ac.WeighControlMethodAc
import com.link.play.fit.ai.utils.DateEx.getDayOfMonth
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class SetUserInfoAc : BaseActivity<AcSetUserInfoBinding>(AcSetUserInfoBinding::inflate) {

    companion object {
        const val Type = "Type"
        const val age = "age"
        const val height = "height"
        const val weight = "weight"
        const val gender = "gender"
        const val goalWeight = "goalWeight"
    }

    var t: String = ""
    val viewModel by lazy {
        ViewModelProvider(this)[SettingViewModel::class.java]
    }

    lateinit var fragment: Fragment

    override fun onCreate(savedInstanceState: Bundle?) {
        useDefaultImmersive()
        super.onCreate(savedInstanceState)
        t = intent.getStringExtra(Type) ?: ""

        initParams()
        initView()
        initData()
    }

    fun initParams() {
        if (!intent.hasExtra(Type)) {
            finish()
            return
        }
    }

    fun initView() {
        binding.ivReturn.setOnClickListener {
            finish()
        }
        lifecycleScope.launch {
            val user = CloudStore.get<UserGuide>(DOC_USER_GUIDE)
            val logWeightBean = CloudStore.getListItem<LogWeightBean>(
                DOC_LOG_WEIGHT, DOC_LOG_WEIGHT, Filter.lessThanOrEqualTo(
                    "createTime", System.currentTimeMillis()
                )
            )
            if (user != null) {
                viewModel.userGuideState.value = user
                viewModel.logWeight.update {
                    it.weightKg = logWeightBean?.weightKg ?: user.weightKg
                    it.weightLbs = logWeightBean?.weightLbs ?: user.weightLbs
                    it.copy()
                }
            }
            when (t) {
                goalWeight -> {
                    fragment = ChooseWeightFg()
                    binding.tvTitle.text = resources.getString(R.string.edit_weight_goal)
                    (fragment as ChooseWeightFg).rulerViewCallBack = {
                        binding.tvTip.visibility = View.VISIBLE
                    }
                }

                age -> {
                    fragment = BornFg()
                    binding.tvTitle.text = resources.getString(R.string.set_birthday)
                }

                weight -> {
//                    fragment = HeightWeightFg()
                    fragment = SettingHeightWeightFg()
                    binding.tvTitle.text = resources.getString(R.string.set_height_weight)
                }


                height -> {
//                    fragment = HeightWeightFg()
                    fragment = SettingHeightWeightFg()
                    binding.tvTitle.text = resources.getString(R.string.set_height_weight)
                }

                gender -> {
                    fragment = GenderFg()
                    binding.tvTitle.text = resources.getString(R.string.set_gender)
                }

                else -> {
                    finish()
                }
            }
            supportFragmentManager.beginTransaction().add(R.id.fragment_container, fragment)
                .commit()
        }
        binding.btnSave.setOnClickListener {
            when (t) {
                goalWeight -> {
                    WeightControlDialog(this).showDialog(
                        nAction = {
                            save()
                        },
                        yAction = {
                            goToAc(WeighControlMethodAc::class.java)
                            save()
                        }
                    )
                }

                else -> {
                    save()
                }
            }
        }
    }

    fun initData() {

    }

    fun save() {
        lifecycleScope.launch {
            CloudStore.set(CloudStore.DOC_USER_GUIDE, viewModel.userGuideState.value)
            CloudStore.addToList(
                DOC_LOG_WEIGHT,
                DOC_LOG_WEIGHT,
                getDayOfMonth(),
                LogWeightBean(
                    viewModel.logWeight.value.weightKg,
                    viewModel.logWeight.value.weightLbs
                )
            )
            finish()
        }
    }
}